package com.zhang.senddeployemail.bug.controller;

import com.zhang.senddeployemail.bug.utils.DomUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext;

import javax.mail.internet.MimeMessage;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BugController
 * @Description
 * @Author zhang_xfei
 * @DATE 2019 2019/3/27 21:36
 **/
@Controller
@RequestMapping("bug")
public class BugController {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private TemplateEngine templateEngine;
    @RequestMapping("mail")
    @ResponseBody
    public String sendEmail(@RequestParam("file") MultipartFile file) throws Exception {
        //解析上传的文件
        InputStream inputStream = file.getInputStream();
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();
        Element rows = elements.get(1);
        List<Element> bugItems = rows.elements();

        //读取模板
        Context context = new Context();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYYMMdd");
        String date =  formatter.format(LocalDate.now());
        context.setVariable("message", "目录地址为："+"/home/zhiban/19duanwu/"+date+"/dsp");
        ArrayList<Map<String, String>> list = new ArrayList<>();
        for (int i = 0; i < bugItems.size(); i++) {
            Element element = bugItems.get(i);
            Map map = DomUtils.dom2Map(element);
            list.add(map);
        }
        context.setVariable("list", list);
        String mailTemplate = templateEngine.process("mailTemplate", context);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("zhangxf3@primeton.com");
//        helper.setTo("1602725012@qq.com");
//        helper.setCc("hufy@primeton.com");
        helper.setTo("haojx@sge.com.cn");
        String[] cCs = new String[]{"yuna@cfss.net.cn", "zhaochen@primeton.com", "zhuxy@sge.com.cn","tuxc@primeton.com","zhangxf3@primeton.com"};
        helper.setCc(cCs);
        helper.setSubject("19年端午版本数据服务平台版本发布地址");
        helper.setText(mailTemplate, true);
        javaMailSender.send(mimeMessage);
        return "success";
    }

    @RequestMapping("file")
    public String sendEmailTest() {
        return "uploadXml";
    }


}
